package com.h2n.framework.composer;

import org.zkoss.zul.*;
import org.zkoss.zkex.zul.West;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zk.ui.Component;
import org.zkoss.zkplus.spring.SpringUtil;
import java.util.List;
import java.util.Iterator;

import com.h2n.framework.dto.ModuleDTO;
import com.h2n.framework.dao.IModuleDAO;

/**
 * Created by IntelliJ IDEA.
 * User: NhatNV
 * Date: Sep 28, 2009
 * Time: 1:34:42 AM
 * To change this template use File | Settings | File Templates.
 */
public class LeftSidebarComposer extends AbstractPageComposer{
  Include xcontents;
  Listbox lstMenu;
  Tree treMenu;
  West titleWestReport,titleWestFunction;

  private void createListMenu(int _memberId, int _parentId) throws Exception{
    IModuleDAO imoDao = (IModuleDAO) SpringUtil.getBean("iModuleDao");
    List<ModuleDTO> liMo = imoDao.getChildByMemberAndParentId(_memberId,_parentId); 
    if(liMo != null && liMo.size() > 0){
      Iterator it = liMo.iterator();
      while(it.hasNext()){
        ModuleDTO moDto = (ModuleDTO)it.next();
        Listitem item = new Listitem();
        item.setValue(moDto);
        item.setHeight("25px");
        item.setLabel(moDto.getModule_Name());
        lstMenu.appendChild(item);
      }
    }
  }

  private void createTreeMenu(Component _parentComp,int _memberId, int _parentId) throws Exception{
    IModuleDAO imoDao = (IModuleDAO) SpringUtil.getBean("iModuleDao");
    List<ModuleDTO> liMo = imoDao.getChildByMemberAndParentId(_memberId,_parentId);
    createTreeMenu(_parentComp,liMo,_parentId);    
  }

  private void createTreeMenu(Component _parentComp,List<ModuleDTO> list,int _parentId){
    if(list != null && list.size() > 0){
      Treechildren child = null;
      Iterator it = list.iterator();
      while(it.hasNext()){
        ModuleDTO moDto = (ModuleDTO)it.next();
        if(moDto.getParent_Id() == _parentId && moDto.getId() != _parentId){
          if(child == null){
             child = new Treechildren();
             child.setParent(_parentComp);
          }

          Treeitem item = new Treeitem();
          item.setValue(moDto);
          item.setParent(child);

          Treerow row = new Treerow();
          row.setParent(item);

          row.appendChild(new Treecell(moDto.getModule_Name()));
//          row.appendChild(new Treecell(moDto.getDescription()));
          createTreeMenu(item,list,moDto.getId());
        }
      }
    }
  }

  public void onCreateLeftMenu(Event e) throws Exception{
    if (lstMenu != null) {
      createListMenu(currentMemberLogined.getId(),Integer.parseInt(getParentId()));
    }
    if (treMenu != null) {
      createTreeMenu(treMenu,currentMemberLogined.getId(),Integer.parseInt(getParentId()));
    }
  }

  public void onSelect$lstMenu(SelectEvent e) throws Exception {
    if (lstMenu.getSelectedItem() != null) {
      ModuleDTO item = (ModuleDTO) lstMenu.getSelectedItem().getValue();
      if (item != null) {
        String url = item.getClass_Path();
        if (url != null && !"".equals(url)){
            url += url.indexOf("?") != -1 ? "&" + PageConstantsFramework.PARAMETER_FUNCTION_ID + "=" + item.getId() : "?" + PageConstantsFramework.PARAMETER_FUNCTION_ID + "=" + item.getId();
            xcontents.setSrc(url);
        }
      }
    }
  }

  public void onSelect$treMenu(SelectEvent e) throws Exception {
    if (treMenu.getSelectedItem() != null) {
      ModuleDTO item = (ModuleDTO) treMenu.getSelectedItem().getValue();
      if (item != null) {
        String url = item.getClass_Path();
        if (url != null && !"".equals(url))
          url += url.indexOf("?") != -1 ? "&" + PageConstantsFramework.PARAMETER_FUNCTION_ID + "=" + item.getId() : "?" + PageConstantsFramework.PARAMETER_FUNCTION_ID + "=" + item.getId();
          xcontents.setSrc(url);
      }
    }
  }

  public void doAfterCompose(Component component)throws Exception{
    super.doAfterCompose(component);
    if(lstMenu != null || treMenu != null){
      if((treMenu == null && lstMenu.getChildren().size() <= 0) || (lstMenu == null && treMenu.getChildren().size() <= 0)){
        Events.postEvent("onCreateLeftMenu", component, null);
      }
    }
  }
}
